package com.lmb.todo.dao;

import com.lmb.todo.pojo.Todo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author lmb
 */
@Mapper
public interface TodoDao {

    /**
     * 根据 groupId 查找 todo
     * @param groupId Integer
     * @return List<Todo>
     */
    @Select("select id, groupId, done, star, content, note, createTime, updateTime, scheduledTime " +
            "from t_todo where groupId=#{groupId}")
    List<Todo> getByGroupId(Integer groupId);

    /**
     * 根据 id 修改 todo 的 done 字段
     * @param id Integer
     * @param done Integer
     * @param updateTime String
     * @return Integer
     */
    @Update("update t_todo set done=#{done}, updateTime=#{updateTime} where id=#{id}")
    Integer updateDone(Integer id, Integer done, String updateTime);

    /**
     * 根据 id 修改 todo 的 star 字段
     * @param id Integer
     * @param star Integer
     * @param updateTime String
     * @return Integer
     */
    @Update("update t_todo set star=#{star}, updateTime=#{updateTime} where id=#{id}")
    Integer updateStar(Integer id, Integer star, String updateTime);

    /**
     * 更新 todo
     * @param todo Todo
     * @return Integer
     */
    @Update("<script> " +
            "update t_todo " +
            "<set> " +
            "<if test='groupId != null'>groupId = #{groupId}, </if> " +
            "<if test='done != null'>done = #{done}, </if> " +
            "<if test='star != null'>star = #{star}, </if> " +
            "<if test='content != null'>content = #{content}, </if> " +
            "<if test='note != null'>note = #{note}, </if> " +
            "<if test='createTime != null'>createTime = #{createTime}, </if> " +
            "<if test='updateTime != null'>updateTime = #{updateTime}, </if> " +
            "<if test='scheduledTime != null'>scheduledTime = #{scheduledTime}, </if> " +
            "</set> " +
            "where id = #{id} " +
            "</script> ")
    Integer update(Todo todo);

    /**
     * 修改 todo 的 groupId
     * @param todoId Integer
     * @param groupId Integer
     * @return Integer
     */
    @Update("update t_todo set groupId=#{groupId} where id=#{todoId}")
    Integer updateGroupId(Integer todoId, Integer groupId);

    /**
     * 删除 todo
     * @param id Integer
     * @return Integer
     */
    @Delete("delete from t_todo where id=#{id}")
    Integer deleteTodo(Integer id);

    /**
    * 新增加 todo
    * @param todo Todo
    * @return Integer
    * */
    @Insert("insert into t_todo(groupId, done, star, content, note, createTime, updateTime, scheduledTime)" +
            "values(#{groupId}, #{done}, #{star}, #{content}, #{note}, #{createTime}, null, #{scheduledTime})")
    Integer add(Todo todo);

    /**
     * 根据t_todo表的id查询groupId
     * @param id Integer
     * @return Integer
     * */
    @Select("select groupId from t_todo where id=#{id}")
    Integer getGroupIdById(Integer id);

    /**
    * 根据t_todo表的content模糊查询表中数据
    * @param content String
    * @param groupIdList List<Integer>
    * @return List<Todo>
    * */
    @Select("<script> " +
            "select id, groupId, done, star, content, note, createTime, updateTime, scheduledTime " +
            "from t_todo where content like \"%\"#{content}\"%\" and groupId in ( " +
            "<foreach collection='groupIdList' item='groupId' separator=','> " +
            "#{groupId} " +
            "</foreach>) " +
            "</script> ")
    List<Todo> getTodoByContent(String content, List<Integer> groupIdList);

}
